//
//  YHAlertPopView.swift
//  FuweiCloud
//
//  Created by michael on 2024/8/22.
//

import UIKit
#if canImport(SnapKit)
import SnapKit
#endif

open class YHAlertPopView: YHPopView {
  
  private let commonHMargin: CGFloat = 20
  private let commonVMargin: CGFloat = 15
  
  open var headerTitle: Any? {
    properties?[.header]
  }
    
  open var messageTitle: Any? {
    properties?[.body]
  }
    
  open var leftButtonTitle: Any? {
    properties?[.leftButton]
  }

  open var rightButtonTitle: Any? {
    properties?[.rightButton]
  }
    
  public private(set) var titleLabel: UILabel!
  public private(set) var messageLabel: UILabel!
    
  public private(set) lazy var leftButton = {
    let btn = UIButton(title: btnTitle(leftButtonTitle, true))
    btn.set(bImage: UIColor.black(2), state: .highlighted)
    return btn
  }()

  public private(set) lazy var rightButton = {
    let btn = UIButton(title: btnTitle(rightButtonTitle, false))
    btn.set(bImage: UIColor.black(2), state: .highlighted)
    return btn
  }()

  private func textHeight(text: Any?, fontSize: CGFloat) -> CGFloat {
    if let text = text as? String {
      text.height(.num(fontSize, .medium), width: contentWidth - commonHMargin*2)
    } else if let text = text as? AString {
      text.height(contentWidth - commonHMargin*2)
    } else {
      0
    }
  }
    
  open func initVariable() {
    contentWidth = properties?[.contentWidth] as? CGFloat ?? 290
    if let userInfo = userInfo as? [AnyHashable: Any] {
      let topMargin: CGFloat = Any2Float(userInfo[YHPopProperty.topMargin]) ?? commonHMargin
      let headerPadding: CGFloat = Any2Float(userInfo[YHPopProperty.headerPadding]) ?? commonVMargin
      let bodyPadding: CGFloat = Any2Float(userInfo[YHPopProperty.bodyPadding]) ?? commonVMargin
      let buttonHeight: CGFloat = Any2Float(userInfo[YHPopProperty.footerHeight]) ?? 45
      let titleHeight = textHeight(text: msgTitle(headerTitle, true), fontSize: 15)
      let messageHeight = textHeight(text: msgTitle(messageTitle, false), fontSize: 13)
      if titleHeight > 0 && messageHeight > 0 {
        contentHeight = topMargin + titleHeight + headerPadding + messageHeight + bodyPadding + buttonHeight
      } else if titleHeight > 0 || messageHeight > 0 {
        contentHeight = topMargin + titleHeight + messageHeight + bodyPadding + buttonHeight
      } else {
        contentHeight = topMargin + bodyPadding + buttonHeight
      }
    } else {
      contentHeight = commonHMargin*2 + 45
    }
  }
    
  open override func initContentUI() {
    initVariable()
    let title = headerTitle
    let message = messageTitle
    
    let topMargin: CGFloat = Any2Float(properties?[YHPopProperty.topMargin]) ?? commonHMargin
    let headerPadding: CGFloat = Any2Float(properties?[YHPopProperty.headerPadding]) ?? commonVMargin
    let buttonHeight: CGFloat = Any2Float(properties?[YHPopProperty.footerHeight]) ?? 45
    if title != nil && message != nil {
      titleLabel = UILabel(string: msgTitle(title, true), lines: 0)
      titleLabel.textAlignment = properties?[.headerAlign] as? NSTextAlignment ?? .center
      messageLabel = UILabel(string: msgTitle(message, false), lines: 0)
      messageLabel.textAlignment = properties?[.headerAlign] as? NSTextAlignment ?? .center
      contentView.addSubviews([titleLabel, messageLabel])
#if canImport(SnapKit)
      titleLabel.snp.makeConstraints { make in
        make.left.equalTo(commonHMargin)
        make.right.equalTo(-commonHMargin)
        make.top.equalTo(topMargin)
      }
      messageLabel.snp.makeConstraints { make in
        make.left.equalTo(commonHMargin)
        make.right.equalTo(-commonHMargin)
        make.top.equalTo(titleLabel.snp.bottom).offset(headerPadding)
      }
#endif
    } else if title != nil || message != nil {
      let isTitle = title != nil
      titleLabel = UILabel(string: msgTitle(title ?? message, isTitle), lines: 0)
      titleLabel.textAlignment = properties?[isTitle ?.headerAlign:.bodyAlign] as? NSTextAlignment ?? .center
      messageLabel = titleLabel
      contentView.addSubviews([titleLabel])
#if canImport(SnapKit)
      titleLabel.snp.makeConstraints { make in
        make.left.equalTo(commonHMargin)
        make.right.equalTo(-commonHMargin)
        make.top.equalTo(topMargin)
      }
#endif
    }
//    let titleTruncated = titleLabel.attributedText!.height(contentWidth - 60) - UIFont.num(15,.semibold).lineHeight > 1
//    titleLabel.textAlignment = titleTruncated ? .left : .center
    leftButton.addAction(.buttonAction, target: self)
    rightButton.addAction(.buttonAction, target: self)
    let line1 = UIView(color: darkMode ?.white(1):.separator)
    let line2 = UIView(color: darkMode ?.white(1):.separator)
    let leftTitle = btnTitle(leftButtonTitle, true)
    let rightTitle = btnTitle(rightButtonTitle, false)
    leftButton.set(title: leftTitle)
    rightButton.set(title: rightTitle)
    if leftTitle != nil, rightTitle != nil {
      contentView.addSubviews([leftButton, rightButton])
    } else if leftTitle != nil {
      line2.isHidden = true
      contentView.addSubviews([leftButton])
    } else if rightTitle != nil {
      line2.isHidden = true
      contentView.addSubviews([rightButton])
    } else {
      line1.isHidden = true
      line2.isHidden = true
    }
    let isSingle = line2.isHidden
#if canImport(SnapKit)
    if leftButton.superview != nil {
      leftButton.snp.makeConstraints { make in
        make.left.bottom.equalTo(0)
        make.height.equalTo(buttonHeight)
        make.width.equalTo(isSingle ?contentWidth:contentWidth/2)
      }
    }
    if rightButton.superview != nil {
      rightButton.snp.makeConstraints { make in
        make.right.bottom.equalTo(0)
        make.height.equalTo(buttonHeight)
        make.width.equalTo(isSingle ?contentWidth:contentWidth/2)
      }
    }
#endif
    contentView.addSubviews([line1, line2])
    let scale = UIScreen.main.scale
#if canImport(SnapKit)
    line1.snp.makeConstraints { make in
      make.left.right.equalTo(0)
      make.bottom.equalTo(-buttonHeight)
      make.height.equalTo(1/scale)
    }
    line2.snp.makeConstraints { make in
      make.bottom.equalTo(0)
      make.height.equalTo(buttonHeight)
      make.width.equalTo(1/scale)
      make.centerX.equalToSuperview()
    }
#endif
  }
    
  private func btnTitle(_ title: Any?, _ isLeft: Bool) -> Any? {
    if let title = title as? String {
      let titleColor: UIColor = darkMode ?.white(6):.label1
      return AString(title, font: .num(15, isLeft ?.regular:.semibold), color: isLeft ?titleColor:Theme.light)
    } else {
      return title
    }
  }
  
  private func msgTitle(_ title: Any?, _ isHeader: Bool) -> Any? {
    if let title = title as? String {
      let titleColor: UIColor = darkMode ?.white(8):.black(8)
      return AString(title, font: .num(isHeader ?15:13, isHeader ?.semibold:.regular), color: titleColor)
    } else {
      return title
    }
  }
    
  @objc
  open func buttonAction(_ btn: UIButton) {
    dismiss(btn == leftButton ? nil:1)
  }
}
